<template>
  <div class="compact-datetime" :class="{'night-mode': isNight}">
    <div class="date-part">
      <span class="day">{{ day }}</span>
      <span class="month">{{ month }}</span>
      <span class="year">{{ year }}</span>
    </div>
    <div class="time-part">
      <span class="hours">{{ hours }}</span>
      <span class="colon">:</span>
      <span class="minutes">{{ minutes }}</span>
      <span class="seconds" v-if="showSeconds">:{{ seconds }}</span>
      <span class="weekday">{{ weekday }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CompactDateTime',
  props: {
    showSeconds: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      currentTime: new Date(),
      timer: null
    }
  },
  computed: {
    day() {
      return this.currentTime.getDate().toString().padStart(2, '0');
    },
    month() {
      const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
      return months[this.currentTime.getMonth()];
    },
    year() {
      return this.currentTime.getFullYear();
    },
    hours() {
      return this.currentTime.getHours().toString().padStart(2, '0');
    },
    minutes() {
      return this.currentTime.getMinutes().toString().padStart(2, '0');
    },
    seconds() {
      return this.currentTime.getSeconds().toString().padStart(2, '0');
    },
    weekday() {
      const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      return weekdays[this.currentTime.getDay()];
    },
    isNight() {
      const hour = this.currentTime.getHours();
      return hour >= 18 || hour < 6;
    }
  },
  mounted() {
    this.updateTime();
    this.timer = setInterval(this.updateTime, 1000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    updateTime() {
      this.currentTime = new Date();
    }
  }
}
</script>

<style scoped>
.compact-datetime {
  width: 270px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  transition: all 0.3s ease;
}

.night-mode {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  color: #ecf0f1;
}

.date-part {
  display: flex;
  align-items: baseline;
  font-weight: bold;
}

.day {
  font-size: 24px;
  margin-right: 4px;
  color: #2c3e50;
}

.night-mode .day {
  color: #ecf0f1;
}

.month {
  font-size: 14px;
  margin-right: 8px;
  color: #7f8c8d;
}

.night-mode .month {
  color: #bdc3c7;
}

.year {
  font-size: 12px;
  color: #95a5a6;
}

.night-mode .year {
  color: #95a5a6;
}

.time-part {
  display: flex;
  align-items: center;
  font-weight: bold;
}

.hours, .minutes, .seconds {
  font-size: 20px;
  color: #2c3e50;
}

.night-mode .hours,
.night-mode .minutes,
.night-mode .seconds {
  color: #ecf0f1;
}

.colon {
  font-size: 20px;
  margin: 0 2px;
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

.weekday {
  font-size: 14px;
  margin-left: 10px;
  color: #7f8c8d;
  padding: 3px 8px;
  background: rgba(127, 140, 141, 0.1);
  border-radius: 10px;
}

.night-mode .weekday {
  color: #bdc3c7;
  background: rgba(189, 195, 199, 0.1);
}
</style>
